/**
 * Copyright © 2019 dream horse Info. Tech Ltd. All rights reserved.
 *
 * @Package: com.github.mybatis.fl.convert
 * @author: flying-cattle
 * @date: 2019年4月9日 下午8:15:25
 */
package com.yz.mybatis.gen.util;

import com.yz.base.boot.entity.JsonResult;
import com.yz.mybatis.gen.entity.BasisInfo;
import com.yz.mybatis.gen.entity.PropertyInfo;

import java.util.List;

/**
 * 获取文件路径调用创建文件.
 *
 * @author <a href="mailto:aiks_yuan@163.com">yuanz</a>
 * @date 2022-02-08 16:30
 * @since 1.0.0
 **/
public class Generator {
    //路径信息
    public static final String ENTITY = "entity";
    public static final String DTO = "dto";
    public static final String DAO = "dao";
    public static final String MAPPER_XML = "mapperXml";
    public static final String SERVICE = "service";
    public static final String SERVICE_IMPL = "serviceImpl";
    public static final String CONTROLLER = "controller";
    
    
    /**
     * 1.创建实体类.
     *
     * @param url dalFileUrl
     * @param bi  bi
     * @return JsonResult
     */
    public static JsonResult createEntity(String url, BasisInfo bi) {
        String fileUrl = getGeneratorFileUrl(url, bi.getEntityUrl(), bi.getEntityName(), ENTITY);
        return FreemarkerUtil.createFile(bi, "entity.ftl", fileUrl);
    }
    
    /**
     * 2.创建DAO.
     *
     * @param url dalFileUrl
     * @param bi  bi
     * @return JsonResult
     */
    public static JsonResult createDao(String url, BasisInfo bi) {
        String fileUrl = getGeneratorFileUrl(url, bi.getDaoUrl(), bi.getEntityName(), DAO);
        return FreemarkerUtil.createFile(bi, "dao.ftl", fileUrl);
    }
    
    /**
     * 3.创建mapper配置文件.
     *
     * @param url dalFileUrl
     * @param bi  bi
     * @return JsonResult
     */
    public static JsonResult createMapperXml(String url, BasisInfo bi) {
        String fileUrl = getGeneratorFileUrl(url, bi.getMapperUrl(), bi.getEntityName(), MAPPER_XML);
        List<PropertyInfo> list = bi.getCis();
        String agile = "";
        for (PropertyInfo propertyInfo : list) {
            agile = agile + propertyInfo.getColumn() + ", ";
        }
        agile = agile.substring(0, agile.length() - 2);
        bi.setAgile(agile);
        return FreemarkerUtil.createFile(bi, "mapper.ftl", fileUrl);
    }
    
    /**
     * 4.创建SERVICE.
     *
     * @param url serviceFileUrl
     * @param bi  bi
     * @return JsonResult
     */
    public static JsonResult createService(String url, BasisInfo bi) {
        String fileUrl = getGeneratorFileUrl(url, bi.getServiceUrl(), bi.getEntityName(), SERVICE);
        return FreemarkerUtil.createFile(bi, "service.ftl", fileUrl);
    }
    
    /**
     * 5.创建SERVICE_IMPL.
     *
     * @param url serviceFileUrl
     * @param bi  bi
     * @return JsonResult
     */
    public static JsonResult createServiceImpl(String url, BasisInfo bi) {
        String fileUrl = getGeneratorFileUrl(url, bi.getServiceImplUrl(), bi.getEntityName(), SERVICE_IMPL);
        return FreemarkerUtil.createFile(bi, "serviceImpl.ftl", fileUrl);
    }
    
    /**
     * 6.创建CONTROLLER.
     *
     * @param url controllerFileUrl
     * @param bi  bi
     * @return JsonResult
     */
    public static JsonResult createController(String url, BasisInfo bi) {
        String fileUrl = getGeneratorFileUrl(url, bi.getControllerUrl(), bi.getEntityName(), CONTROLLER);
        return FreemarkerUtil.createFile(bi, "controller.ftl", fileUrl);
    }
    
    /**
     * 7.创建实体类.
     *
     * @param url serviceFileUrl
     * @param bi  bi
     * @return JsonResult
     */
    public static JsonResult createEntityDTO(String url, BasisInfo bi) {
        String fileUrl = getGeneratorFileUrl(url, bi.getEntityDtoUrl(), bi.getEntityName(), DTO);
        return FreemarkerUtil.createFile(bi, "entityDTO.ftl", fileUrl);
    }
    
    /**
     * 生成文件.
     *
     * @param url 文件路径
     * @param packageUrl 包路径
     * @param entityName 实体名
     * @param type 文件type
     * @return
     */
    public static String getGeneratorFileUrl(String url, String packageUrl, String entityName, String type) {
        if (type.equals("entity")) {
            return url + pageToUrl(packageUrl) + entityName + ".java";
        } else if (type.equals("dto")) {
            return url + pageToUrl(packageUrl) + entityName + "DTO.java";
        } else if (type.equals("dao")) {
            return url + pageToUrl(packageUrl) + entityName + "Dao.java";
        } else if (type.equals("mapperXml")) {
            return url + packageUrl + entityName + "Mapper.xml";
        } else if (type.equals("service")) {
            return url + pageToUrl(packageUrl) + entityName + "Service.java";
        } else if (type.equals("serviceImpl")) {
            return url + pageToUrl(packageUrl) + entityName + "ServiceImpl.java";
        } else if (type.equals("controller")) {
            return url + pageToUrl(packageUrl) + entityName + "Controller.java";
        }
        return null;
    }
    
    public static String pageToUrl(String url) {
        return url.replace(".", "/") + "/";
    }
}
